﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   Allows player to select level
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game.UIScripts
{
    class LevelSelection : Panel
    {
        private readonly UIController _ui;

        private readonly ContentManager _contentManager;
        private MouseState _previousMouseState;

        private readonly List<UIButton> _buttons = new List<UIButton>();
        private string _selectedButtonName = "";

        private Texture2D _logo;

        public LevelSelection(IServiceProvider serviceProvider, GraphicsDevice graphicsDevice, ContentManager content, UIController controller)
        {
            _contentManager = content;
            _ui = controller;
            LoadContent();
        }

        public void Reset()
        {

        }

        private void LoadContent()
        {
            _logo = _contentManager.Load<Texture2D>("Sprites/UI/LevelSelection/logo");
            _buttons.Add(new UIButton("1", new Vector2(150, 165), _contentManager.Load<Texture2D>("Sprites/UI/LevelSelection/1"), true));
            _buttons.Add(new UIButton("2", new Vector2(150, 235), _contentManager.Load<Texture2D>("Sprites/UI/LevelSelection/2"), true));
            _buttons.Add(new UIButton("back", new Vector2(15, 15), _contentManager.Load<Texture2D>("Sprites/UI/LevelSelection/back"), true));
        }


        public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState)
        {
            if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Released)
            {
                //When clicked
                foreach(UIButton btn in _buttons)
                {
                    if (checkCollision(new Vector2(mouseState.X, mouseState.Y), btn))
                    {
                        Console.WriteLine("Clicked "+ btn.Name);
                        
                        _selectedButtonName = btn.Name;
                        _previousMouseState = mouseState;
                        ButtonClicked(_selectedButtonName);
                        break;
                    }
                }
            }
            else if (mouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton == ButtonState.Pressed)
            {
                //When released
                _selectedButtonName = "";
                _previousMouseState = mouseState;
            }
            else if (mouseState.LeftButton == ButtonState.Pressed && _previousMouseState.LeftButton == ButtonState.Pressed)
            {
                _previousMouseState = mouseState;
            }
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(_texture);
            spriteBatch.Begin();
            spriteBatch.Draw(_logo, new Vector2(0,0), Color.White);
            foreach (UIButton btn in _buttons)
            {
                spriteBatch.Draw(btn.Texture, btn.Position, Color.White);
            }
            
            spriteBatch.End();
        }

        private void ButtonClicked(string name)
        {
            switch (name)
            {
                case "1":
                    _ui.SetLevel("test");
                    _ui.ChangePanel(UIController.PanelType.GameScreen, this);
                    break;
                case "2":
                    _ui.SetLevel("test2");
                    _ui.ChangePanel(UIController.PanelType.GameScreen, this);
                    break;
                case "back":
                    _ui.ChangePanel(UIController.PanelType.MainMenu, this);
                    break;
                default:
                    break;
            }
        }

        private bool checkCollision(Vector2 mousePos, UIButton btn)
        {
            if (mousePos.X >= btn.Position.X // To the right of the left side
            && mousePos.X < btn.Position.X + btn.Texture.Width //To the left of the right side
            && mousePos.Y > btn.Position.Y //Below the top side
            && mousePos.Y < btn.Position.Y + btn.Texture.Height) //Above the bottom side
                return true; //We are; return true.
            else
                return false; //We're not; return false.
        }

    }
}
